<?php
//兑换产品发布、管理
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/4 0004
 * Time: 10:02
 */
namespace Wx\Controller;
use Wx\Model\ExchangeQrcodeModel;
class StoreExchangeController extends CommonController{
	//发布、编辑兑换产品
	public function index(){
		$exchange_goods=D('exchange_goods');
		$this->assign("u_id",session('u_id'));
        $category = M('category');
        //获取相关信息
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $this->assign('list_category',$list_category);

		$g_id=$_POST['id']; //dump($g_id);die;
		if($_POST['u_id']>0){
			//说明在提交数据
			//新增产品
			$data=$_POST;
			
			//处理多余的逗号		
			$data['head_img']=preg_replace('/(,)+/i',',',$_POST['head_img']);
			$data['head_img']=rtrim($data['head_img'],',');//去尾
			$data['head_img']=ltrim($data['head_img'],',');//去头
	
			$data['con_img']=preg_replace('/(,)+/i',',',$_POST['con_img']);
			$data['con_img']=rtrim($data['con_img'],',');//去尾
			$data['con_img']=ltrim($data['con_img'],',');//去头
			
			if($g_id==="0"){
				
				//添加时间
				$data['addtime']=time();
				//dump($data);die;
				$alt=$exchange_goods->add($data);
				if ($alt){
					$this->redirect('StoreExchange/lis');
				}else{
					$this->error('添加失败');
				}
			}else{
				//更新产品
				$data['state']=0;
				$data['uptime']=time();
				$alt=$exchange_goods->where("id=".$g_id)->save($data);
				if ($alt){
					$this->redirect('StoreExchange/lis');
				}else{
					$this->error('更新失败');
				}
			}
		}//end if
		
		$g_id=$_GET['id']; //dump($g_id);die;
		if($g_id>0){
			//展示要编辑的产品内容
			$goods=$exchange_goods->where("id=".$g_id)->find();
				//图片分拆
				//首页
				$img=explode(',',$goods['head_img']);
				$goods['img1']=$img[0];
				$goods['img2']=$img[1];
				//内容
				$img=explode(',',$goods['con_img']);
				$goods['img3']=$img[0];
				$goods['img4']=$img[1];
				$goods['img5']=$img[2];
				$goods['img6']=$img[3];
			
			$this->assign('goods',$goods);
		}//end if
		
		$this->display('addproduct-dh');
	}//end fun
	//审核状态 审核中02 上架1 下架4
	public function lis(){
		$exchange_goods=D('exchange_goods');
        $category = M('category');
		$state=$_GET['state'];
		$where='';
		if($state=='02')$where=" and (state=0 or state=2)";
		if($state=='1')$where=" and state=1";
		if($state=='4')$where=" and state=4";
		$goods=$exchange_goods->where("state!=3 and u_id=".session('u_id').$where)->order('id desc')->select();
		foreach($goods as  $k=>$one){
			$img=explode(',',$one['head_img']);
			$goods[$k]['f_img']=$img[0];//第一张图片作为封面图
			$goods[$k]['unit']=$category->where("id=".$one['unit'])->getField('title');
		}//end foreach
        $this->assign('goods',$goods);
		
		$this->display('proaduit');
	}//end fun 
	public function Del(){
        $exchange_goods = M("exchange_goods");
        $sgid = I("post.id");
        if ($sgid){
            $delete = $exchange_goods->where("id = {$sgid}")->save(['state'=> 3]);
            if (!$delete){
                $this->error("删除失败");
            }
        }
	}//end fun
}//end class